package com.lq.ecoldchain.ui.register

import android.arch.lifecycle.MutableLiveData
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.MD5
import com.lq.ecoldchain.util.toast

/*
*2018/12/23 0023  19:24
*function by lq
*/
class RegisterViewModel : BaseViewModel() {
    val pwdCanSee by lazy { MutableLiveData<Boolean>() }
    val againPwdCanSee by lazy { MutableLiveData<Boolean>() }

    val phone by lazy { MutableLiveData<String>() }
    val verificationCode by lazy { MutableLiveData<String>() }
    val user by lazy { MutableLiveData<String>() }
    val pwd by lazy { MutableLiveData<String>() }
    val pwdAgain by lazy { MutableLiveData<String>() }
    val invitationCode by lazy { MutableLiveData<String>() }

    val isSending by lazy { MutableLiveData<Boolean>() }

    val map by lazy { HashMap<String,String>() }

    fun register() {
        if (phone.value.isNullOrEmpty() || verificationCode.value.isNullOrEmpty() || user.value.isNullOrEmpty() || pwd.value.isNullOrEmpty() || pwdAgain.value.isNullOrEmpty()){
            toast { "请填写完整信息" }
        }else{
            map["phone"] = phone.value!!
            map["code"] = verificationCode.value!!
            map["userName"] = user.value!!
            map["loginPassword"] = MD5.encode(pwd.value!!)
            if(!invitationCode.value.isNullOrEmpty()){
                map["inviteCode"] = invitationCode.value!!
            }
            BaseNetCore.ApiServiceImpl.register(map).compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<String>(){
                override fun onSuccess(t: String?) {}

                override fun onFail(code: String) {}
            })
        }
    }

    fun sendCode() {
        if (phone.value.isNullOrEmpty()) {
            toast { "请填写手机号码" }
        } else {
           BaseNetCore.ApiServiceImpl.sendRegisterCode(phone.value!!).compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<String>(){
               override fun onSuccess(t: String?) {}
               override fun onFail(code: String) {}
           })
        }
    }
}